﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Itop.GpsCar.Model;
using Ext.Net;
using TreeNode = Ext.Net.TreeNode;

namespace Itop.GpsCar.Web.MainMap
{
    public partial class main : System.Web.UI.Page
    {     
        protected void Page_Load(object sender, EventArgs e)
        {   
            SiteMapNode siteNode = SiteMap.RootNode;
            TreeNode root = this.CreateNode(siteNode);
            root.Draggable = false;
            root.Expanded = true;
            TreePanel1.Root.Add(root);
        }

        public void TrackingClick()
        {
            string url;
            /*url = "Tracking.aspx?deviceid=" + */
        

            /*Response.Redirect();*/
        }

        //static node creation with children
        private Ext.Net.TreeNode CreateNode(SiteMapNode siteMapNode)
        {
            TreeNode treeNode = new TreeNode();

            treeNode.NodeID = siteMapNode.Key;
            treeNode.Text = siteMapNode.Title;
            treeNode.Qtip = siteMapNode.Description;

            SiteMapNodeCollection children = siteMapNode.ChildNodes;

            //if (children != null && children.Count > 0)
            //{
            //    foreach (SiteMapNode mapNode in siteMapNode.ChildNodes)
            //    {
            //        treeNode.Nodes.Add(this.CreateNode(mapNode));
            //    }
            //}
            System.Collections.Generic.IList<device> deviceList = Global.SqlMapper.GetList<device>(null);
            foreach (device de in deviceList)
            {
                System.Collections.Generic.IList<devicecarrier> carrierList = Global.SqlMapper.GetList<devicecarrier>(" where devicecarrier.device_id = " + de.device_id);
                foreach (devicecarrier dc in carrierList)
                {
                    System.Collections.Generic.IList<carrier> carList = Global.SqlMapper.GetList<carrier>(" where idCarrier = " + dc.device_id);
                    foreach (carrier car in carList)
                    {
                        TreeNode tempNode = new TreeNode();
                        tempNode.NodeID = de.device_id.ToString();
                        tempNode.Text = car.plate;
                        tempNode.Qtip = de.device_id.ToString();
                        treeNode.Nodes.Add(tempNode);
                    }
                }
            }

            return treeNode;
        } 

    }
}
